package com.healthtop.dao;

import com.healthtop.mapper.XsMenuMapper;
import com.healthtop.model.XsMenu;
import com.healthtop.model.XsMenuExample;
import com.healthtop.utils.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class XsMenuDao {

    @Autowired
    private XsMenuMapper  xsMenuMapper;

    public XsMenu selectById(Long id) {
        return xsMenuMapper.selectByPrimaryKey(id);
    }

    public void update(XsMenu xsMenu) {
        xsMenuMapper.updateByPrimaryKeySelective(xsMenu);
    }

    public void insert(XsMenu xsMenu) {
        xsMenuMapper.insertSelective(xsMenu);
    }

    public List<XsMenu> selectParentMenu() {
        XsMenuExample example = new XsMenuExample();
        example.createCriteria().andParentIdEqualTo(0l).andDelFalgEqualTo("0");
        return xsMenuMapper.selectByExample(example);
    }

    public List<XsMenu> selectMenuByPid(Long menuId) {
        XsMenuExample example = new XsMenuExample();
        example.createCriteria().andParentIdEqualTo(menuId).andDelFalgEqualTo("0");
        return xsMenuMapper.selectByExample(example);
    }

    public XsMenu getMenuByPath(String servletPath) {
        XsMenuExample example = new XsMenuExample();
        example.createCriteria().andPathEqualTo(servletPath).andDelFalgEqualTo("0");
        return CollectionUtils.getFirst(xsMenuMapper.selectByExample(example));
    }
}
